package com.starbaba.pushservice.service.impl;

import java.io.File;
import java.io.FileInputStream;

import org.springframework.stereotype.Component;

import com.jumai.server.qiniu.QiniuUploadUtils;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.starbaba.core.service.CommonServiceSupport;
import com.starbaba.pushservice.model.PushActionModel;
import com.starbaba.pushservice.service.PushActionService;

/**
 * 推送后台的处理逻辑实现
 * @author wangzhuobin
 *
 */
@Component
public class PushActionServiceImpl extends CommonServiceSupport implements
		PushActionService {

	private static final long serialVersionUID = 1111269882972611330L;

	/**
	 * 上传消息图标的接口
	 * @param model
	 * @return
	 */
	@Override
	public String uploadMessageIcon(PushActionModel model) {
		String url = null;
		if (model != null) {
			File file = model.getMessageIcon();
			if (file != null) {
				url = uploadFileToQiniu(file);
			}
		}
		return url;
	}

	/**
	 * 上传文件到7牛服务器的接口
	 * @param file
	 * @return
	 */
	@Override
	public String uploadFileToQiniu(File file) {
		String url = null;
		if (file != null) {
			if (file.isDirectory()) {
				return url;
			} else {
				try {
					FileInputStream in = new FileInputStream(file);
					// Zone为七牛机房所在地，根据源所在机房配置
			        Configuration config = new Configuration(Zone.zone0()); 
			        url = QiniuUploadUtils.updateImg("push", in, null, config);
			        in.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return url;
	}

}
